<?php
class CompareLang
{

	var $original_lang = 'en';
	var $compared_lang;
	
	var $original_lang_files = array();
	var $compared_lang_files = array();
	
	var $lang_name;
	
	var $multi_ini_files = array('common_hint.ini',
                                 'service_msg.ini',
	                             'knowledgebase/hint_msg.ini',
                                 'knowledgebase/setting_description_msg.ini',
								 'file/hint_msg.ini',
                                 'email_setting/hint_msg.ini');
								 
	var $diff = array('missed_files' => array(),
	                  'missed_fields' => array(),
					  'equal_fields' => array());
	
	
	function CompareLang($compared_lang) {
		$this->setComparedLang($compared_lang);
		$this->setFiles();
		
		$this->lang_name = $this->getLangName();
	}
		
	
	function &getMissedFiles() {
		
		$b = $this->_doArrayDiff($this->original_lang_files, $this->compared_lang_files);
		
		$this->deleteEmpty($b);
		$this->diff['missed_files'] = &$b;
		return $b;
	} 
	
	
	// two dimensinal only
	function &getMissedFields($b = array()) {
		
		foreach($this->original_lang_files as $k => $v) {
			
			if(is_array($v)) {
				
				foreach($v as $k1 => $v1) {
					$b[$k][$v1] = $this->_getMissedFields($k . '/' . $v1);
				}
			
			} else {
				$b[$v] = $this->_getMissedFields($v);
			}
		}
		
		$this->deleteEmpty($b);
		$this->diff['missed_fields'] = &$b;
		return $b;
	}
	
	
	// two dimensinal only
	function &getEqualValues() {
		
		$b = array();
		foreach($this->original_lang_files as $k => $v) {
			
			if(is_array($v)) {
				
				foreach($v as $k1 => $v1) {
					$b[$k][$v1] = $this->_getEqualValues($k . '/' . $v1);					
				}
			
			} else {
				
				$b[$v] = $this->_getEqualValues($v);		
			}
		}

		$this->deleteEmpty($b);
		$this->diff['equal_fields'] = &$b;
		return $b;
	}	
	
	
	function setComparedLang($lang) {
		$this->compared_lang = $lang;
	}
	
	
	function setFiles() {
		 
		$d = new MyDir;
		$d->setSkipDirs('.svn');
		$d->setSkipFiles($this->original_lang . '/config_lang.php');
		$d->setAllowedExtension('ini');
		$d->one_level = false;
		$d->full_path = false;
		
		$this->original_lang_files = $d->getFilesDirs($this->original_lang);
		$this->_doSort($this->original_lang_files);
		
		$this->compared_lang_files = $d->getFilesDirs($this->compared_lang);
		$this->_doSort($this->compared_lang_files);
	}
	
	
	function getLangName() {
		$conf = array();
		require_once $this->compared_lang . '/config_lang.php';
		return $conf['lang']['name'];
	}
	
	
	function _getEqualValues($file) {
		
		$file1 = $this->original_lang . '/' . $file;
		$file2 = $this->compared_lang . '/' . $file;
		
		if(file_exists($file2)) { 
		
			if(in_array($file, $this->multi_ini_files)) {
				$arr1 = $this->_parseMultiIni($file1);
				$arr2 = $this->_parseMultiIni($file2);		
			} else {
				$arr1 = parse_ini_file($file1, 1);
				$arr2 = parse_ini_file($file2, 1);			
			}
			
			return $this->_doArrayIntersect($arr1, $arr2);				
		}
	}
	
	
	function _doArrayIntersect($arr1, $arr2, $a = array()) {
		
		foreach($arr1 as $k => $v) {
			if(is_array($arr1[$k])) {
				if(!isset($arr2[$k])) { continue; }
				$a[$k] = $this->_doArrayIntersect($arr1[$k], $arr2[$k]);
			}
		}

		if(!isset($arr2)) { continue; }
		return $this->_array_intersect($arr1, $arr2) + $a;
		return array_intersect($arr1, $arr2) + $a;		
		//return array_merge(array_intersect($arr1, $arr2), $a);
	}
	
	
	// own array_intersect function
	function _array_intersect($arr1, $arr2, $a = array()) {
		foreach($arr1 as $k => $v) {
			if(@$arr1[$k] == @$arr2[$k]) {
				$a[$k] = $v;
			}
		}
		
		return $a;
	}
		
	
	function _getMissedFields($file) {
		
		$file1 = $this->original_lang . '/' . $file;
		$file2 = $this->compared_lang . '/' . $file;
	
		if(file_exists($file2)) { 
		
			if(in_array($file, $this->multi_ini_files)) {
				$arr1 = $this->_parseMultiIni($file1);
				$arr2 = $this->_parseMultiIni($file2);					
			} else {
				$arr1 = parse_ini_file($file1, 1);
				$arr2 = parse_ini_file($file2, 1);			
			}
			
			return $this->_doArrayDiff($arr1, $arr2, 1);			
		}
	}
	
	
	// recursive array_diff
	function _doArrayDiff($arr1, $arr2, $keys = false, $a = array()) {
		
		foreach($arr1 as $k => $v) {
			if(is_array($arr1[$k])) {
				
				if(!isset($arr2[$k])) { $arr2[$k] = array(); }				
				$a[$k] = $this->_doArrayDiff($arr1[$k], $arr2[$k], $keys);
			}
		}

		if(!isset($arr2)) { $arr2 = array(); }
		
		if($keys) {
			$arr1 = array_keys($arr1);
			$arr2 = array_keys($arr2);
		}
		
		return array_diff($arr1, $arr2) + $a;
		//return array_merge(array_diff($arr1, $arr2), $a);
	}
	
	
	// asort multi array
	function _doSort(&$arr) {
		
		foreach($arr as $k => $v) {
			if(is_array($arr[$k])) {
				//$this->_doSort($arr[$k]);
			}
		}
		
		//asort($arr);
	}
	
	
	function deleteEmpty(&$arr) {
		
		foreach($arr as $k => $v) {
			if(is_array($arr[$k])) {			
				$this->deleteEmpty($arr[$k]);
			} 
			
			if(empty($arr[$k])) { unset($arr[$k]); }
		}
	}
	
	
	// parse multilines ini file
	// it will skip all before defining first [block]
	function & _parseMultiIni($file, $key = false) {
		$s_delim = '[';
		$e_delim = ']'; 
		
		$str = implode('',file($file));
		if($key && strpos($str, $s_delim . $key . $e_delim) === false) { return; } 
		
		$str = explode($s_delim, $str);
		$num = count($str);
			
		for($i=1;$i<$num;$i++){
			$section = substr($str[$i], 0, strpos($str[$i], $e_delim));
			$arr[$section] = substr($str[$i], strpos($str[$i], $e_delim)+strlen($e_delim));
		}
		
		return ($key) ? @$arr[$key] : $arr;
	}	
	
	
	function getLangs() {

		$d = new MyDir;
		$d->setSkipDirs('.svn');
		$d->setSkipDirs('en');
		$d->setAllowedExtension('ini');
		$d->one_level = false;
		$d->full_path = false;

		$arr = array();		
		foreach($d->getFilesDirs('.') as $k => $v) {
			if(is_string($k)) {
				$arr[] = $k;
			}
		}
		
		return $arr;
	}
	
	
	function getHTML() {
		
		$html[] = '<b>Compared language: '.$this->lang_name.'</b><hr size="1" noshade><br>';		
		$html[] = '<table cellpadding="3" cellspacing="1" width="100%">';
		
		
		// missed files
		$html[] = '<tr><td colspan="3"><b>Missed files</b></td></tr>';
		$html[] = '<tr bgcolor="#c6c6c6"><td colspan="3">File</td></tr>';		
		
		if(!$this->diff['missed_files']) {
			$html[] = '<tr bgcolor="#f1f1f1"><td colspan="3">No missed files</td></tr>';					
		} else {
			$i = 0;
			foreach($this->diff['missed_files'] as $file_name => $v) {
				if(is_array($v)) { $file = $file_name . '/' . current($v); next($v); } 
				else             { $file = $v; }
				
				$html[] = '<tr bgcolor="#f1f1f1"><td colspan="3">'.$file.'</td></tr>';					
			}
		}
		
		
		// missed fields
		$html[] = '<tr><td colspan="3"><br></td></tr>';	
		$html[] = '<tr><td colspan="3"><b>Missed fields</b></td></tr>';		
		
		$html[] = '<tr bgcolor="#c6c6c6">';
		$html[] = '<td>File</td>';
		$html[] = '<td>Field</td>';
		$html[] = '<td>Sub fields</td>';
		$html[] = '</tr>';		
		
		if(!$this->diff['missed_fields']) {
			$html[] = '<tr bgcolor="#f1f1f1"><td colspan="3">No missed fields</td></tr>';
		} else {
			
			foreach($this->diff['missed_fields'] as $dir => $v) {
				
				// directory
				if(is_array($v) && !strpos($dir, '.')) { 
					
					foreach($v as $file_name => $fields_arr) {
					
						foreach($fields_arr as $msg_key => $values) {
							
							$file = $dir . '/' . $file_name; 
							
							// one value
							if(is_array($values)) {
								$field = $msg_key;
								$sub_field = implode(', ', $values);
							
							// sub fields
							} else {
								$field = $values;
								$sub_field = "''";
							}
							
							$html[] = '<tr bgcolor="#f1f1f1">';
							$html[] = '<td>'.$file.'</td>';
							$html[] = '<td>'.$field.'</td>';
							$html[] = '<td>'.$sub_field.'</td>';				
							$html[] = '</tr>';
						}
					}	
				
				// file
				} else { 
									
					foreach($v as $msg_key => $values) {
						
						$file = $dir; 
						
						// one value
						if(is_array($values)) {
							$field = $msg_key;
							$sub_field = implode(', ', $values);
						
						// sub fields
						} else {
							$field = $values;
							$sub_field = "''";
						}
						
						$html[] = '<tr bgcolor="#f1f1f1">';
						$html[] = '<td>'.$file.'</td>';
						$html[] = '<td>'.$field.'</td>';
						$html[] = '<td>'.$sub_field.'</td>';				
						$html[] = '</tr>';
					}
				}
			}
		}		
		
		
		
		// equal fields
		$html[] = '<tr><td colspan="3"><br></td></tr>';	
		$html[] = '<tr><td colspan="3"><b>Equal fields</b></td></tr>';		
			
		$html[] = '<tr bgcolor="#c6c6c6">';
		$html[] = '<td>File</td>';
		$html[] = '<td>Field</td>';
		$html[] = '<td>Value</td>';
		$html[] = '</tr>';		
		
		if(!$this->diff['equal_fields']) {
			$html[] = '<tr bgcolor="#f1f1f1"><td colspan="3">No equal fields</td></tr>';
		} else {
			
			foreach($this->diff['equal_fields'] as $dir => $v) {
				
				// directory
				if(is_array($v) && !strpos($dir, '.')) { 
					
					foreach($v as $file_name => $fields_arr) {
					
						foreach($fields_arr as $msg_key => $values) {
							
							$file = $dir . '/' . $file_name; 
							
							// values array
							if(is_array($values)) {
								$field = $msg_key;
								$sub_field = implode(', ', $values);
								
								foreach($values as $key => $value) {
									
									$field = $msg_key . '[' . $key . ']';
									$sub_field = $value;
									
									$html[] = '<tr bgcolor="#f1f1f1">';
									$html[] = '<td>'.$file.'</td>';
									$html[] = '<td>'.$field.'</td>';
									$html[] = '<td>'.$sub_field.'</td>';				
									$html[] = '</tr>';									
									
								}
							
							} else {
								
								$field = $msg_key;
								$sub_field = $values;
								
								$html[] = '<tr bgcolor="#f1f1f1">';
								$html[] = '<td>'.$file.'</td>';
								$html[] = '<td>'.$field.'</td>';
								$html[] = '<td>'.$sub_field.'</td>';				
								$html[] = '</tr>';
							}
						}
					}	
				
				// file
				} else { 
									
					foreach($v as $msg_key => $values) {
						
						$file = $dir; 
						
						// values array
						if(is_array($values)) {
							$field = $msg_key;
							$sub_field = implode(', ', $values);
							
							foreach($values as $key => $value) {
								
								$field = $msg_key . '[' . $key . ']';
								$sub_field = $value;
								
								$html[] = '<tr bgcolor="#f1f1f1">';
								$html[] = '<td>'.$file.'</td>';
								$html[] = '<td>'.$field.'</td>';
								$html[] = '<td>'.$sub_field.'</td>';				
								$html[] = '</tr>';									
								
							}
						
						} else {
							
							$field = $msg_key;
							$sub_field = $values;
							
							$html[] = '<tr bgcolor="#f1f1f1">';
							$html[] = '<td>'.$file.'</td>';
							$html[] = '<td>'.$field.'</td>';
							$html[] = '<td>'.$sub_field.'</td>';				
							$html[] = '</tr>';
						}
					}
				}
			}
		}
		
		
		$html[] = '<table>';
		
		return implode("\n", $html);		
	}
	
	
	
	function getLanguageHTML() {
		
		$html[] = '<b>Language: '.$this->lang_name.'</b><br>
		           (there is a list of allmessages in '.$this->lang_name.' language).<br>
				   It could	be useful for spell checking.			   
				   <hr size="1" noshade><br>';		
		$html[] = '<table cellpadding="3" cellspacing="1" width="100%">';
		$html[] = '<tr><td colspan="3"><b></b></td></tr>';	
		
			
		foreach($this->diff['equal_fields'] as $dir => $v) {
			
			// directory
			if(is_array($v) && !strpos($dir, '.')) { 
				
				foreach($v as $file_name => $fields_arr) {
				
					foreach($fields_arr as $msg_key => $values) {
						
						//$file = $dir . '/' . $file_name; 
						
						// values array
						if(is_array($values)) {
							$field = $msg_key;
							$sub_field = implode(', ', $values);
							
							foreach($values as $key => $value) {
								
								//$field = $msg_key . '[' . $key . ']';
								$sub_field = $value;
								
								$html[] = '<tr bgcolor="#f1f1f1">';
								$html[] = '<td colspan="3">'.$sub_field.'</td>';				
								$html[] = '</tr>';									
								
							}
						
						} else {
							
							//$field = $msg_key;
							$sub_field = $values;
							
							$html[] = '<tr bgcolor="#f1f1f1">';
							$html[] = '<td colspan="3">'.$sub_field.'</td>';				
							$html[] = '</tr>';
						}
					}
				}	
			
			// file
			} else { 
								
				foreach($v as $msg_key => $values) {
					
					$file = $dir; 
					
					// values array
					if(is_array($values)) {
						$field = $msg_key;
						$sub_field = implode(', ', $values);
						
						foreach($values as $key => $value) {
							
							//$field = $msg_key . '[' . $key . ']';
							$sub_field = $value;
							
							$html[] = '<tr bgcolor="#f1f1f1">';
							$html[] = '<td colspan="3">'.$sub_field.'</td>';				
							$html[] = '</tr>';									
							
						}
					
					} else {
						
						//$field = $msg_key;
						$sub_field = $values;
						
						$html[] = '<tr bgcolor="#f1f1f1">';
						$html[] = '<td colspan="3">'.$sub_field.'</td>';				
						$html[] = '</tr>';
					}
				}
			}
		}
		
		
		$html[] = '<table>';
		
		return implode("\n", $html);		
	}
}
?>
